package com.xiaou.es.demo;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.Log4J2LoggerFactory;

/**
 * es的简单的增删改操作
 * @author xiaou
 * @date 2018年9月14日-上午10:13:30
 */
public class EasyCrud {
	private TransportClient client;
	private static InternalLogger log =Log4J2LoggerFactory.getInstance(EasyCrud.class);
	
	@SuppressWarnings("resource")
	@Before
	public void before() throws UnknownHostException {
		
		Settings settings = Settings.builder()
				.put("cluster.name", "myes")
				.build();
		client = new PreBuiltTransportClient(settings)
				.addTransportAddress(
						new TransportAddress(InetAddress.getByName("192.168.150.142"), 9300)
						);
	}
	
	@After
	public void after() {
		client.close();
	}
	
	// 根据ID进行查询
	@Test
	public void test1() {
		
		GetResponse get = client.prepareGet("xiaou", "test", "1")
								.execute()
								.actionGet();
		System.out.println(get.getSourceAsString());
	}
	
	// 添加文档
	@Test
	public void test2() throws IOException {
		
		XContentBuilder doc =XContentFactory.jsonBuilder()
							.startObject()
							.field("title", "redis")
							.field("price", 12)
							.endObject();
		IndexResponse indexResponse = client.prepareIndex("xiaou", "test", "7")
											.setSource(doc)
											.get();
		
		log.info(indexResponse.status().toString());
	}
	
	// 删除文档
	@Test
	public void test3() {
		
		DeleteResponse delete = client.prepareDelete("xiaou", "test", "7").get();
		
		log.info(delete.status().toString());
		
	}
	
	// 更新文档
	@Test
	public void Test4() throws Exception {
		UpdateRequest updateReq = new UpdateRequest();
		XContentBuilder doc = XContentFactory.jsonBuilder()
							  .startObject()
							  .field("price",38)
							  .endObject();
		updateReq.index("xiaou")
				 .type("test")
				 .id("1")
				 .doc(doc);
		UpdateResponse updateRes = client.update(updateReq).get();
		
		log.info(updateRes.status().toString());
	}
	
	// upsert更新文档
	@Test
	public void Test5() throws Exception{
		
		IndexRequest index = new IndexRequest("xiaou", "test", "7")
								 .source(
										 XContentFactory.jsonBuilder()
										  .startObject()
										  .field("title","javascript")
										  .field("price",38)
										  .endObject()
										 );
		UpdateRequest update = new UpdateRequest("xiaou", "test", "7")
								   .doc(XContentFactory.jsonBuilder()
										   	.startObject()
										   	.field("price",55)
										   	.endObject())
								   .upsert(index);
		UpdateResponse updateRes = client.update(update).get();
		
		log.info(updateRes.status().toString());
		
	}
}
